#!/usr/bin/env bash

# Check whether "sage" can be run (i.e. whether it's in the PATH)
if ! sage -v >/dev/null; then
    echo >&2 "Error: $0 requires 'sage' to be in your PATH"
    echo >&2 "Maybe first call 'SAGE_ROOT/sage -sh'"
    exit 1
fi

echo "Trying to commit changes (if any)..."
sage -hg commit
status=$?
if [ $status -eq 1 ]; then
    # 'hg commit' returns 1 if there are no changes to commit...
    echo "No changes to commit (not an error)"
elif [ $status -ne 0 ]; then
    echo >&2 "Error: $0: Couldn't commit changes" \
             "('hg commit' failed with exit code $status)"
    exit 1
else
    # $status == 0
    echo "Successfully committed the changes"
fi

echo "Running 'python setup.py sdist'..."
sage -python setup.py sdist
if [ $? -ne 0 ]; then
    echo >&2 "Error: $0: 'python setup.py sdist' failed"
    exit 1
fi


echo "**********************************************************"
echo "* If this is an official SageNB release, don't forget to *"
echo "* push the changes to the public Mercurial repository.   *"
echo "**********************************************************"
